﻿//-----------------------------------------------------------------------
// <copyright file="TumblrBasic.cs" company="OpenSource">
//     All code is Microsoft Public License (Ms-PL)
// </copyright>
// <history>
// 2013.02  Created         Chris Hand
// </history>
//-----------------------------------------------------------------------
namespace OpenSource.TumblrAPI.ApiV2
{
    using System;
    using System.Collections.Generic;
    using OpenSource.TumblrAPI.Interfaces;
    using OpenSource.TumblrAPI.Support;
    using OpenSource.TumblrAPI.DataObjects;

    /// <summary>
    /// Handles basic Tumblr commands
    /// </summary>
    public class TumblrBasic : ITumblrBasic
    {
        /// <summary>
        /// The client to use
        /// </summary>
        private ITumblrClient client;

        /// <summary>
        /// The logget to use
        /// </summary>
        private ILogger log;

        private ITumblrParser parser;


        /// <summary>
        /// Initializes a new instance of the TumblrBasic class
        /// </summary>
        /// <param name="client">The client to use when talking to Tumblr</param>
        /// <param name="parser">The object that will parse the response</param>
        /// <param name="logger">The logger to use when logging information</param>
        public TumblrBasic(ITumblrClient client, ITumblrParser parser, ILogger logger)
        {
            this.client = client;
            this.log = logger;
            this.parser = parser;
        }

        /// <summary>
        /// Gets posts with the specified tag
        /// </summary>
        /// <param name="tag">The tag to look for</param>
        /// <returns>A collection of TumblrPosts, or an empty collection if there were none</returns>
        public OpenSource.TumblrAPI.DataObjects.TumblrPost[] RequestPostsWithTag(string tag)
        {
            this.log.Trace("RequestPostsWithTag({0})", tag);
            string command = "tagged?";
            IDictionary<string, object> args = new Dictionary<string, object>();
            args.Add("tag", tag);
            string result = this.client.Request(command, args);
            return this.parser.Parse(result);
        }

        /// <summary>
        /// Gets posts with the specified tag
        /// </summary>
        /// <param name="tag">The tag to look for.</param>
        /// <param name="before">Find posts before this date</param>
        /// <param name="limit">The maximum number of posts to return</param>
        /// <param name="format">What format the posts should be in</param>
        /// <returns>The collection of posts</returns>
        public OpenSource.TumblrAPI.DataObjects.TumblrPost[] RequestPostsWithTag(string tag, DateTime before, int limit, PostFormat format)
        {
            this.log.Trace("RequestPostsWithTag({0}, {1}, {2}, {3})", tag, before, limit, format);
            string command = "tagged?";
            IDictionary<string, object> args = new Dictionary<string, object>();
            args.Add("tag", tag);
            args.Add("before", Util.ComputeTimeStampFromLocalTime(before));
            args.Add("limit", limit);

            if (format != PostFormat.Html)
            {
                args.Add("filter", format.ToString().ToLower());
            }

            string result = this.client.Request(command, args);
            return this.parser.Parse(result); 
        }
    }
}
